var form;
var layer;
//页面初始化时加载Layui框架
layui.use(['form', 'layer', 'laydate'], function () {
    form = layui.form();
    layer = layui.layer;
    $.removeTableCookie('SpryMedia_DataTables_jobTable_jobList.jsp');
    getDataTable();

    //新增
    $("#addJob").click(function () {
        saveOrUpdateJob(null);
    });
    // 查询
    $("#search").click(function () {
        $.removeTableCookie('SpryMedia_DataTables_jobTable_jobList.jsp');
        getDataTable(); //要触发的方法
        return false;
    });

    //回车事件
    $(document).keydown(function (event) {
        var code = event.which;
        if (code == 13) {
            $.removeTableCookie('SpryMedia_DataTables_jobTable_jobList.jsp');
            getDataTable(); //要触发的方法
            return false;
        }
    });
});
/**
 * 获取列表
 */
function getDataTable() {
    $('#jobTable').dataTable({
        'bProcessing': true,
        'bServerSide': true,
        'fnServerParams': function (aoData) {
            var customerKeyWord = $.trim($("#customerKeyWord").val());
            var jobCode = $.trim($("#jobCode").val());
            var jobState = $("#jobState").val();
            var startTime = $.trim($("#startTime").val());
            var endTime = $.trim($("#endTime").val());
            aoData.push({
                "name": "job.customerKeyWord",
                "value": customerKeyWord
            }, {
                "name": "job.code",
                "value": jobCode
            }, {
                "name": "job.state",
                "value": jobState
            }, {
                "name": "job.startTime",
                "value": startTime
            }, {
                "name": "job.endTime",
                "value": endTime
            });
        },
        "sAjaxSource": basePath + "job/getJobList.action", // 获取列表
        "sServerMethod": "POST",
        "sPaginationType": "full_numbers",
        "bPaginate": true, // 翻页功能
        "bStateSave": true, // 状态保存
        "bLengthChange": false, // 改变每页显示数据数量
        "bFilter": false, // 过滤功能
        "bSort": false, // 排序功能
        "bInfo": true, // 页脚信息
        "bAutoWidth": false, // 自动宽度
        "bDestroy": true,
        "iDisplayLength": 15, // 每页显示多少行
        "aoColumns": [{
            "sTitle": '排序',
            "mDataProp": "no"
        }, {
            "sTitle": '工单编号',
            "mDataProp": "jobCode"
        }, {
            "sTitle": '客户名称',
            "mDataProp": "customerName",
            "sClass": "conceal"
        }, {
            "sTitle": '联系方式',
            "mDataProp": "customerContact"
        }, {
            "sTitle": '地址',
            "mDataProp": "customerAddress",
            "sClass": "conceal"
        }, {
            "sTitle": '客户级别',
            "mDataProp": "customerLevel"
        }, {
            "sTitle": '紧急状态',
            "mDataProp": "stateStr"
        }, {
            "sTitle": '受理人',
            "mDataProp": "createUserName"
        }, {
            "sTitle": '受理时间',
            "mDataProp": "createTime"
        }, {
            "sTitle": '操作',
            "mDataProp": null
        }],
        "oLanguage": {
            "sUrl": basePath + "plugins/datatable/cn.txt" // 中文包
        },
        "fnDrawCallback": function (oSettings) {
            $('#jobTable tbody  tr td').each(function () {
                this.setAttribute('title', $(this).text());
            });
        },
        "fnInitComplete": function () {
        },
        "aoColumnDefs": [{
            "aTargets": [2],
            "fnRender": function (oObj) {
                var cusId = oObj.aData.customerId;
                var cusName = oObj.aData.customerName;
                return '<a href="javascript:void(0);" onclick=detailCustomer("' + cusId + '")>' + cusName + '</a>';
            }
        },{
            "aTargets": [9],
            "fnRender": function (oObj) {
                var jobId = oObj.aData.id;
                var jobCode = oObj.aData.jobCode;
                return '<a data-method="authorize" class="layui-btn layui-btn-small layui-btn-problem layui-btn-radius" onclick=jobDetails(\'' + jobId + '\',\'' + jobCode + '\')>问题产品</a>'
                    + '<a data-method="repair" class="layui-btn layui-btn-small layui-btn-normal layui-btn-radius" onclick=saveOrUpdateJob(\'' + jobId + '\')>修改</a>'
                    + '<a data-method="del" class="layui-btn layui-btn-small layui-btn-danger layui-btn-radius" onclick=deleteJob(\'' + jobId + '\')>删除</a>';
            }
        }]
    });
}

function detailCustomer(cusId){
    var url = "customer/toUpdateOrDetail.action?type=detail&id=" + cusId;
    layer.open({
        type: 2,
        title: '查看客户信息',
        area: ["50%", "65%"],
        shade: 0.3,
        maxmin: true,
        content: basePath + url,
        btn: ['关闭'],
        yes: function (index) {
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        },
        zIndex: layer.zIndex,
        success: function (layero) {
            layer.setTop(layero); //弹窗置于最上层
        }
    });
}

/**
 * 跳转到问题产品页面
 */
function jobDetails(roleId, jobCode) {
    var url = "jsp/jobProduct/jobProductList.jsp?id=" + roleId + "&jobCode=" + jobCode;
    var titleVal = "问题产品";
    layer.open({
        type: 2,
        title: titleVal,
        area: ["100%", "100%"],
        shade: 0.3,
        content: basePath + url,
        zIndex: layer.zIndex,
        success: function (layero, index) {//弹出后回调
            layer.setTop(layero); //弹窗置于最上层
            layer.full(index);
        }
    });
}


/**
 * 新增/修改
 */
function saveOrUpdateJob(jobId) {
    var url;
    var titleVal;
    if (null == jobId) {
        url = "jsp/job/addOrUpdateJob.jsp";
        titleVal = "新增工单";
    } else {
        url = "job/toUpdate.action?type=update&id=" + jobId;
        titleVal = "修改工单";
    }
    layer.open({
        type: 2,
        title: titleVal,
        area: ["50%", "45%"],
        shade: 0.3,
        maxmin: true,
        content: basePath + url,
        zIndex: layer.zIndex,
        success: function (layero) {//弹出后回调
            layer.setTop(layero); //弹窗置于最上层
        }
    });
}

/**
 * 删除
 */
function deleteJob(jobId) {
    layer.confirm('删除此工单数据会同事删除对应的工单产品记录和处理历史记录,确定删除此条数据?', {
        title: " 确认",
        icon: 3,
        skin: 'layui-layer-molv',
        btn: ["确定", "取消"],
        yes: function (index) {
            $.ajax({
                url: basePath + "job/deleteJob.action?id=" + jobId,
                type: "post",
                dataType: 'text',
                success: function (data) {
                    if (data != null && data != "" && data == 0) {
                        getDataTable();
                    } else {
                        layer.open({
                            title: '错误',
                            icon: 2,
                            content: '删除失败！'
                        });
                        getDataTable();
                    }
                }
            });
            layer.close(index);
        }
    });
}